﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using MongoDB.Bson.Serialization; 

namespace Uranus.Common
{
    public class Utility
    {
        #region 通过Key获得AppSettings值
        /// <summary>
        /// 通过Key得到Appsettings值
        /// </summary>
        /// <param name="key">App</param>
        /// <returns></returns>
        public static string AppSettings(string key)
        {
            return ConfigurationManager.AppSettings[key].ToString();
        }
        #endregion

    }
    /// <summary>
    /// 这个类型实现了对JSON数据处理的一些扩展方法
    /// </summary>
    public static class JsonExtensions
    {
        /// <summary>
        /// 根据一个字符串，进行JSON的反序列化，转换为一个特定类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool toObject<T>(this string data, out T t)
        {
            t = default(T);

            try
            {
                t = JsonConvert.DeserializeObject<T>(data);

                return true;
            }
            catch
            {
                return false;
            }

            //t = default(T);

            //try
            //{
            //    var serializer = new DataContractJsonSerializer(typeof(T));

            //    var ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
            //    t = (T)serializer.ReadObject(ms);

            //    ms.Close();

            //    return true;
            //}
            //catch
            //{
            //    return false;
            //}
        }


        /// <summary>
        /// 将json数据反序列化为Object
        /// </summary>
        /// <param name="jsonData">json数据</param>
        /// <returns></returns>
        public static bool JsonToObject<T>(string jsonData,out T t)
        {
            t = default(T);
            //实例化JavaScriptSerializer类的新实例
            var jss = new JavaScriptSerializer();
            try
            {
                //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
                //t = jss.Deserialize<T>(jsonData);
                //BsonDocument document = Serialization.BsonSerializer.Deserialize<BsonDocument>(jsonData);
                t = BsonSerializer.Deserialize<T>(jsonData);
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 将任何一个对象转换为JSON字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool toJson<T>(this T obj, out string result)
        {
            result = string.Empty;
            try
            {
                result = JsonConvert.SerializeObject(obj);

                return true;
            }
            catch
            {
                return false;
            }
            //result = string.Empty;

            //try
            //{
            //    var serializer = new DataContractJsonSerializer(typeof(T));

            //    var ms = new MemoryStream();
            //    serializer.WriteObject(ms, obj);

            //    ms.Position = 0;
            //    var sr = new StreamReader(ms);
            //    result = sr.ReadToEnd();

            //    sr.Close();
            //    ms.Close();

            //    return true;
            //}
            //catch
            //{
            //    return false;
            //}
        }
        /// <summary>
        /// 将json数据反序列化为Dictionary
        /// </summary>
        /// <param name="jsonData">json数据</param>
        /// <returns></returns>
        public static Dictionary<string, object> JsonToDictionary(string jsonData)
        {
            //实例化JavaScriptSerializer类的新实例
            var jss = new JavaScriptSerializer();
            try
            {
                //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
                return jss.Deserialize<Dictionary<string, object>>(jsonData);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 序列化
        /// </summary>
        public static string Serialize(object obj)
        {
            if (obj != null)
                return new JavaScriptSerializer().Serialize(obj);
            else return string.Empty;
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T DeSerialize<T>(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return new JavaScriptSerializer().Deserialize<T>(str);
            }
            else
            {
                return default(T);
            }
        }
    }
}
